/**
 * 
 */
package org.tea.springdata.web.utils;

import org.apache.http.client.HttpClient;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

/**
 * @author John zhang
 * @version 0.1
 */
public class HttpClientUtils {

	public static final String HTTP_HEADER = "http:";

	protected static HttpClient httpclient;

	protected static boolean isDebug = true;
	
	private static void createClient () {
		HttpParams params = new BasicHttpParams ();
		HttpConnectionParams.setConnectionTimeout ( params, 20000 );
		HttpConnectionParams.setSoTimeout ( params, 25000 );
		HttpConnectionParams.setSocketBufferSize ( params, 8192 );

		SchemeRegistry schReg = new SchemeRegistry ();
		schReg.register ( new Scheme ( "http", 80, PlainSocketFactory.getSocketFactory ()  ) );
		schReg.register ( new Scheme ( "https", 443, SSLSocketFactory.getSocketFactory () ) );

		httpclient = new DefaultHttpClient ( new PoolingClientConnectionManager ( schReg ), params );
	}
	
	public static HttpClient getHttpClient () {
		if ( httpclient == null ) {
			createClient();
		}
		return httpclient;
	}
	
}
